package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.modles.RelationBrandData;

/**
 * 关联商品页面用于显示搜索的商品的ListView的适配器
 * 通过品牌名搜索时的适配器
 * Created by lanouhn on 16/9/14.
 */
public class RelationBrandAdatper extends BaseAdapter {
    private Context context;
    private RelationBrandData brandData;

    public RelationBrandAdatper(Context context, RelationBrandData brandData) {
        this.context = context;
        this.brandData = brandData;
    }

    @Override
    public int getCount() {
        return brandData.getData().size();
    }

    @Override
    public Object getItem(int position) {
        return brandData.getData().get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_brand_listview_relationproduct, null);
            holder = new ViewHolder();
            holder.iv_product_item = (ImageView) convertView.findViewById(R.id.iv_product_item);
            holder.tv_brandname_item = (TextView) convertView.findViewById(R.id.tv_brandname_item);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(context).load(brandData.getData().get(position).getPic()).config(Bitmap.Config.RGB_565).into(holder.iv_product_item);
        holder.tv_brandname_item.setText(brandData.getData().get(position).getName());
        return convertView;
    }

    public class ViewHolder {
        private ImageView iv_product_item;// 商品的小图
        private TextView tv_brandname_item;// 商品的品牌名
    }
}
